package org.suwenqian.audiovideosummary.config;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "qiniu")
public class QiNiuYunProps {

    private String ak;

    private String sk;

    private String bucket;

    private String domain;

    private String region;

    /**
     * 配置七牛云存储区域
     */
    @Bean
    public com.qiniu.storage.Configuration qiniuConfiguration() {
        // 自动识别区域
        return new com.qiniu.storage.Configuration(Region.autoRegion());
    }

    /**
     * 上传管理器
     */
    @Bean
    public UploadManager uploadManager(com.qiniu.storage.Configuration configuration) {
        return new UploadManager(configuration);
    }

    /**
     * 认证管理器
     */
    @Bean
    public Auth auth() {
        return Auth.create(ak, sk);
    }

    /**
     * 空间管理器
     */
    @Bean
    public BucketManager bucketManager(Auth auth, com.qiniu.storage.Configuration configuration) {
        return new BucketManager(auth, configuration);
    }

    /**
     * 获取上传凭证
     */
    public String getUploadToken() {
        return auth().uploadToken(bucket);
    }
}
